package com.ruoyi.information.controller.pc;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.information.domain.pc.ApplyContact;
import com.ruoyi.information.service.pc.IApplyContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 一键联系Controller
 *
 * @author ruoyi
 * @date 2023-05-26
 */
@RestController
@RequestMapping("/contact")
public class ApplyContactController extends BaseController {
    @Autowired
    private IApplyContactService applyContactService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询一键联系列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ApplyContact applyContact) {
        startPage();
        List<ApplyContact> list = applyContactService.selectApplyContactList(applyContact);
        return getDataTable(list);
    }


    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(applyContactService.selectApplyContactById(id));
    }

    /**
     * 新增一键联系
     */
    @PostMapping
    public AjaxResult add(@RequestBody ApplyContact applyContact) {
        /*判断手机号是否存在*/
        if (!applyContact.getPhone().isEmpty() && !applyContact.getPhone().equals("")) {
            /*判断验证码是否存在*/
            if (!applyContact.getCode().isEmpty() && !applyContact.getCode().equals("")) {
                /*获取redis里的验证码*/
                String codes = redisService.getCacheObject("connectCode:" + applyContact.getPhone());
                if (codes != null) {
                    if (!applyContact.getCode().equals(codes)) {
                        return AjaxResult.error("验证码不正确");
                    }
                } else {
                    return AjaxResult.error("验证码已过期，请重新获取");
                }
            } else {
                return AjaxResult.error("验证码不能为空");
            }

        } else {
            return AjaxResult.error("手机号不能为空");
        }
        return toAjax(applyContactService.insertApplyContact(applyContact));
    }

    /**
     * 修改一键联系
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ApplyContact applyContact) {
        return toAjax(applyContactService.updateApplyContact(applyContact));
    }

    /**
     * 删除一键联系
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(applyContactService.deleteApplyContactByIds(ids));
    }
}
